package game.algorithms;

import models.Board;

/**
 * AbstractGameAlgorithms is a class for putting common algorithms used by the game
 *
 * @author finni
 * @version 1.0
 *          Created 24.11.2011 14:54
 */
public abstract class AbstractGameAlgorithms implements IGameAlgorithms
{
    /***
     * gameEnded is a static method that checks if a game has ended
     * @param myBoard holds the session board object and info about the game being played
     * @return a boolean value true if game has ended, false otherwise
     */
    public boolean gameEnded(Board myBoard)
    {
        if (myBoard.getMark(0) != '-')
        {
            //line 0 - 1 - 2
            if (myBoard.getMark(0) == myBoard.getMark(1) && myBoard.getMark(0) == myBoard.getMark(2))
                return true;

            //line 0 - 3 - 6
            if (myBoard.getMark(0) == myBoard.getMark(3) && myBoard.getMark(0) == myBoard.getMark(6))
                return true;
        }

        if (myBoard.getMark(8) != '-')
        {
            //line 8 - 5 - 2
            if (myBoard.getMark(8) == myBoard.getMark(5) && myBoard.getMark(8) == myBoard.getMark(2))
                return true;

            //line 8 - 7 - 6
            if (myBoard.getMark(8) == myBoard.getMark(7) && myBoard.getMark(8) == myBoard.getMark(6))
                return true;
        }

        if (myBoard.getMark(4) != '-')
        {
            //line from middle 1 - 4 - 7
            if (myBoard.getMark(4) == myBoard.getMark(1) && myBoard.getMark(4) == myBoard.getMark(7))
                return true;

            //line from middle 3 - 4 - 5
            if (myBoard.getMark(4) == myBoard.getMark(3) && myBoard.getMark(4) == myBoard.getMark(5))
                return true;

            //line from middle 6 - 4 - 2
            if (myBoard.getMark(4) == myBoard.getMark(6) && myBoard.getMark(4) == myBoard.getMark(2))
                return true;

            //line from middle 0 - 4 - 8
            if (myBoard.getMark(4) == myBoard.getMark(0) && myBoard.getMark(4) == myBoard.getMark(8))
                return true;
        }

        //Auðvitað á þessi að vera neðst ef þetta er síðasta svæðið og mylla
        return myBoard.isFull();
    }
}
